package com.tbit.uqbike.client.controller;

import com.tbit.uqbike.client.constant.ResponseCode;
import com.tbit.uqbike.client.constant.RuleRidelogConstant;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.pojo.User;
import com.tbit.uqbike.client.pojo.newEntity.RuleRidePhotolog;
import com.tbit.uqbike.client.pojo.newEntity.RuleRidelog;
import com.tbit.uqbike.client.service.RuleRidelogService;
import com.tbit.uqbike.client.service.TokenService;
import com.tbit.uqbike.client.util.Assert;
import com.tbit.uqbike.client.util.OutputUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 非文明骑行记录
 *
 * @Author: chen
 * @Description:
 * @Date: 创建于 14:59 2022/7/12
 */
@Controller
@RequestMapping("/ruleRidelog")
public class RuleRidelogController {

    @Autowired
    private RuleRidelogService ruleRidelogService;
    @Autowired
    private TokenService tokenService;

    /**
     * 申诉
     *
     * @param ruleRidelog 申诉对象
     * @param request
     * @param response
     * @param session
     */
    @RequestMapping("/edit")
    public void edit(RuleRidelog ruleRidelog, HttpServletRequest request, HttpServletResponse response,
                     HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        try {
            Assert.notNull(ruleRidelog.getOrderNo(), "-3064", "订单编号不能为空");
            Assert.notNull(ruleRidelog.getReason(), "-110001", "申请理由不能为空");
            ruleRidelog.setState(RuleRidelogConstant.RULERIDELOG_APPEAL);
            ruleRidelogService.edit(ruleRidelog);
            map.put(ResponseCode.RET, ResponseCode.SUCC);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }
        OutputUtil.renderText(request, response, map);
    }

    /**
     * 查询非文明骑行列表
     *
     * @param token    鉴权令牌
     * @param request
     * @param response
     * @param session
     */
    @RequestMapping("/query")
    public void query(String token, HttpServletRequest request, HttpServletResponse response,
                      HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        try {
            User user = tokenService.getUser(token);
            List<RuleRidelog> ruleRidelogs = ruleRidelogService.query(user.getUserId());
            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, ruleRidelogs);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }
        OutputUtil.renderText(request, response, map);
    }

    /**
     * 查询非文明骑行图片
     *
     * @param rideId   非文明骑行记录id
     * @param request
     * @param response
     * @param session
     */
    @RequestMapping("/queryPhoto")
    public void queryPhoto(Integer rideId, HttpServletRequest request, HttpServletResponse response,
                           HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        try {
            Assert.notNull(rideId, "-180001", "主键id不能为空");
            List<RuleRidePhotolog> ruleRidelogs = ruleRidelogService.queryPhoto(rideId);
            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, ruleRidelogs);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }
        OutputUtil.renderText(request, response, map);
    }

    /**
     * 通过id查询
     *
     * @param rideId
     * @param mapType
     * @param request
     * @param response
     * @param session
     */
    @RequestMapping("/getByRideId")
    public void getByRideId(Integer rideId, @RequestParam(defaultValue = "2") Integer mapType, HttpServletRequest request, HttpServletResponse response,
                            HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        try {
            Assert.notNull(rideId, "-180001", "主键id不能为空");
            RuleRidelog ruleRidelog = ruleRidelogService.getByRideId(map, rideId, mapType);
            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, ruleRidelog);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }
        OutputUtil.renderText(request, response, map);
    }

    /**
     * 通过订单号查询非文明骑行详情
     *
     * @param orderNO  订单编号
     * @param mapType  地图类型
     * @param request
     * @param response
     * @param session
     */
    @RequestMapping("/queryRuleRideLog")
    public void queryRuleRideLog(String orderNO, @RequestParam(defaultValue = "2") Integer mapType, HttpServletRequest request, HttpServletResponse response,
                                 HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        try {
            Assert.notNull(orderNO, "-3064", "订单编号不能为空");
            List<RuleRidelog> ruleRidelog = ruleRidelogService.queryRuleRideLog(map, orderNO, mapType);
            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, ruleRidelog);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }
        OutputUtil.renderText(request, response, map);
    }

}
